﻿using System.IO;
using System.Windows;
using System.Reflection;
using System.Windows.Controls;

using Builderator.Core.Action;

namespace Builderator.Firefly.Controls
{
    /// <summary>
    /// Interaction logic for BuildVersionControl.xaml
    /// </summary>
    public partial class BuildVersionControl : UserControl, IProperty
    {
        public static DependencyProperty VersionProperty = DependencyProperty.Register("Version", typeof(BuildVersion), typeof(BuildVersionControl));

        public new event PropertyChanged OnPropertyChanged;

        public PropertyInfo Property
        {
            get;
            set;
        }

        public BuildVersion Version
        {
            get
            {
                return (BuildVersion)this.GetValue(VersionProperty);
            }
            set
            {
                this.SetValue(VersionProperty, value);

                this.Parse(value);

                if (this.OnPropertyChanged != null)
                    this.OnPropertyChanged(this.Property, value);
            }
        }

        private BuildVersionControl()
        {
            InitializeComponent();
        }

        public BuildVersionControl(PropertyInfo property)
            : this()
        {
            this.Property = property;
        }

        private void Parse(BuildVersion value)
        {
            this.majorTextBox.Text = value.Major.ToString();

            this.minorTextBox.Text = value.Minor.ToString();

            this.buildTextBox.Text = value.Build.ToString();

            this.revisionTextBox.Text = value.Revision.ToString();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.majorTextBox.Text != string.Empty)
            {
                this.Version.Major = int.Parse(this.majorTextBox.Text);
            }

            if (this.minorTextBox.Text != string.Empty)
            {
                this.Version.Minor = int.Parse(this.minorTextBox.Text);
            }

            if (this.buildTextBox.Text != string.Empty)
            {
                this.Version.Build = int.Parse(this.buildTextBox.Text);
            }

            if (this.revisionTextBox.Text != string.Empty)
            {
                this.Version.Revision = int.Parse(this.revisionTextBox.Text);
            }

            this.Version = this.Version;
        }
    }
}